package com.example.exam.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.exam.mapper.PictureMapper;
import com.example.exam.pojo.Picture;
import com.example.exam.service.PictureService;
import com.example.exam.utils.SnowFlakeUtil;
import org.springframework.stereotype.Service;

/**
 * @author zhozixiong
 * @date 2023/05/05
 */
@Service
public class PictureServiceImpl extends ServiceImpl<PictureMapper, Picture> implements PictureService {

    @Override
    public String insert(String type, byte[] data) {
        //通过雪花算法生成文件名称,生成文件名称
        String pictureName = SnowFlakeUtil.getSnowFlakeId() + "." + type;
        Picture picture = new Picture(pictureName, data);
        baseMapper.insert(picture);
        return pictureName;
    }

    @Override
    public Picture searchAllByPictureName(String pictureName) {
        QueryWrapper<Picture> wrapper = new QueryWrapper<>();
        wrapper.eq("picture_name", pictureName);
        return baseMapper.selectOne(wrapper);
    }
}
